package com.tcm.order.service.task;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.core.service.ConfigService;
import com.tcm.doctor.domain.dto.DoctorDTO;
import com.tcm.doctor.service.DoctorService;
import com.tcm.order.domain.dto.OrderBillTaskDTO;
import com.tcm.order.dao.mapper.OrderDoctorBillMapper;
import com.tcm.order.entity.OrderDoctorBill;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class OrderDoctorBillTask implements OrderBillTaskFactory {

    @Autowired
    OrderDoctorBillMapper orderDoctorBillMapper;

    @DubboReference(version = "0.0.1-alpha", retries = 3, timeout = 6000)
    private ConfigService configService;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private DoctorService doctorService;

    @Override
    public void create(OrderBillTaskDTO orderDoctorBillDTO) {

        DoctorDTO doctorDTO = doctorService.findDoctorByUserId(orderDoctorBillDTO.getDoctorId());
        if(ObjectUtil.isNotNull(doctorDTO)){
            //获得订单信息
            BigDecimal billingCommission = new BigDecimal("0");
            switch (doctorDTO.getJobtitleLevel().intValue()){
                case 1 :
                    billingCommission = NumberUtil.mul(new BigDecimal(configService.findValue("first_brokerage", "0.5")),
                            orderDoctorBillDTO.getBillingMoney());
                    break;
                case 2 :
                    billingCommission = NumberUtil.mul(new BigDecimal(configService.findValue("second_brokerage", "0.7")),
                            orderDoctorBillDTO.getBillingMoney());
                    break;
                case 3 :
                    billingCommission = NumberUtil.mul(new BigDecimal(configService.findValue("third_brokerage", "0.8")),
                            orderDoctorBillDTO.getBillingMoney());
                    break;
            }
            //药费提成
            BigDecimal drugsCommission = NumberUtil.mul(new BigDecimal(configService.findValue("drug_brokerage", "0.3")),
                    NumberUtil.mul(orderDoctorBillDTO.getDrugMoney(),orderDoctorBillDTO.getQuantity()));

            //总服务费 = 服务费 * 帖数
            BigDecimal totalServiceMoney = NumberUtil.mul(orderDoctorBillDTO.getServiceMoney(),orderDoctorBillDTO.getQuantity());
            //服务费提成
            BigDecimal serviceCommission = NumberUtil.mul(new BigDecimal(configService.findValue("settlement_service", "1")),
                    totalServiceMoney);


            OrderDoctorBill doctorOrderBill = new OrderDoctorBill();
            doctorOrderBill.setId(uidGenerator.getUID());
            doctorOrderBill.setUserId(orderDoctorBillDTO.getUserId());
            doctorOrderBill.setOrderId(orderDoctorBillDTO.getId());
            doctorOrderBill.setOrderTotals(orderDoctorBillDTO.getTotalMoney());
            doctorOrderBill.setBillingMoney(orderDoctorBillDTO.getBillingMoney());
            doctorOrderBill.setServiceMoney(orderDoctorBillDTO.getServiceMoney());
            doctorOrderBill.setTotalServiceMoney(totalServiceMoney);
            doctorOrderBill.setDrugsMoney(NumberUtil.mul(orderDoctorBillDTO.getDrugMoney(),orderDoctorBillDTO.getQuantity()));
            doctorOrderBill.setDrugMoney(orderDoctorBillDTO.getDrugMoney());
            doctorOrderBill.setQuantity(orderDoctorBillDTO.getQuantity().intValue());
            doctorOrderBill.setProcessCost(orderDoctorBillDTO.getProcessCost());
            doctorOrderBill.setBillingCommission(billingCommission);
            doctorOrderBill.setServiceCommission(serviceCommission);
            doctorOrderBill.setDrugsCommission(drugsCommission);
            doctorOrderBill.setTotalCommission(NumberUtil.add(billingCommission,serviceCommission,drugsCommission));
            doctorOrderBill.setState("0");
            doctorOrderBill.setDecoctingFee(orderDoctorBillDTO.getDecoctingFee());
            doctorOrderBill.setCreateTime(LocalDateTime.now());
            doctorOrderBill.setUpdateTime(LocalDateTime.now());
            orderDoctorBillMapper.insert(doctorOrderBill);
        }

    }
}
